//
//  YKHomeViewController.m
//  yuke
//
//  Created by Dejun Liu on 15/1/5.
//  Copyright (c) 2015年 juwan. All rights reserved.
//

#import "YKHomeViewController.h"
#import "YKHomeTableViewCell.h"
#import "YKMonthPlace.h"
#import "YKPlace.h"

@interface YKHomeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *tData;
@property (strong, nonatomic) IBOutlet UIView *sectionHeaderView;

@end

@implementation YKHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"首页";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49-64) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"YKHomeTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:self.tableView];
    

    UIImageView *headerImageView = [[UIImageView alloc] init];
    headerImageView.backgroundColor = [UIColor grayColor];
    [headerImageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"demo2"]];
    headerImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*(338.0f/640.0f));
    self.tableView.tableHeaderView = headerImageView;
    
    [self.tableView addHeaderWithTarget:self action:@selector(requestData)];
    [self.tableView headerBeginRefreshing];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark request data

- (void)requestData
{
//    __weak YKHomeViewController *weakSelf = self;
//    [YKProgress ShowProgressInView:self.view];
    [[JWWebService sharedInstance]getWithParams:@{@"module":@"index"} jsonResponse:^(id responseData, NSError *error) {
//        [YKProgress HideProgressInView:weakSelf.view];
        [self.tableView headerEndRefreshing];
        if (responseData) {
            UIImageView *imageView =(UIImageView *) self.tableView.tableHeaderView;
            [imageView sd_setImageWithURL:[NSURL URLWithString:[[[responseData objectForKey:@"headpic"] objectAtIndex:0] objectForKey:@"pic"]]];
            _tData = [YKMonthPlace listFromArrayDic:[responseData objectForKey:@"list"]];
            [_tableView reloadData];
        }
    }];
}

#pragma mark tableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tData.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.sectionHeaderView.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YKMonthPlace *mp = [self.tData objectAtIndex:indexPath.row];
    static NSString *identifier = @"Cell";
    YKHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.month.text = [NSString stringWithFormat:@"%@",mp.month];
//    [cell.itemsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//        YKCircleView *cView = obj;
//        cView.lbl.text = @"";
//        cView.userInteractionEnabled = NO;
//        [cView.imgView sd_setImageWithURL:[NSURL URLWithString:@""]];
//    }];
    
//    for (int i=0; i<mp.items.count; i++) {
//        YKPlace *p = [mp.items objectAtIndex:i];
//        YKCircleView *circleView = [cell.itemsArray objectAtIndex:i];
//        circleView.lbl.text = p.name;
//        circleView.userInteractionEnabled = YES;
//        [circleView.imgView sd_setImageWithURL:[NSURL URLWithString:p.pic]];
//    }
//    cell.scrollView.contentSize = CGSizeMake(mp.items.count *(76 + 10)+10 , cell.height);
    
    cell.listData =  mp.items;
    [cell.collectionView reloadData];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"did selected");
}

@end
